<?php
/*
Plugin Name: Google Friend Connect Integration
Plugin URI: http://www.socialarrow.com/google-friend-connect-integration-wordpress/
Description: Easily integrate Google Friend Connect with Wordpress.  To get started, please enter your Google Friend Connect Site Id number in the <a href="options-general.php?page=google-friend-connect-integration/gfcintegration.php">GFC Integration admin page</a> and choose whether to turn on the social bar or not.  You can add Google Friend Connect gadgets to your sidebars by visiting the <a href="widgets.php">widgets page</a>.  For help and support see the official <a href="http://www.socialarrow.com/google-friend-connect-integration-wordpress/">Google Friend Connect integration for WordPress page</a>.
Version: 0.9.7.4
Author: Social Mind
Author URI: http://www.socialarrow.com/social-arrow/
*/
/*  Copyright 2009  SocialMind  (email : socialmind@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
define(GFCI_MEMBER_WIDGET, "mem_widget_gfci");
define(GFCI_LOGIN_WIDGET, "login_widget_gfci");

require(dirname(__FILE__).'/wallgadget.php');
require(dirname(__FILE__).'/reviewgadget.php');
require(dirname(__FILE__).'/logingadget.php');
require(dirname(__FILE__).'/membergadget.php');
require(dirname(__FILE__).'/customgadget.php');

function gfci_header() {
	?>
	<script type="text/javascript" src="http://www.google.com/friendconnect/script/friendconnect.js"></script>
	<?php
}

function gfci_sit() {
	$gfci_val = get_option( gfci_side_id );
	echo $gfci_val;
}

function gfc_plugin_menu() {
	add_options_page('GFC Integration', 'GFC Integration', 8, __FILE__, 'gfci_options_page');
}

function gfci_options_page() {
	$gfci_opt = 'gfci_side_id';
	$gfci_social_bar = 'gfci_social_bar';
	$gfci_canvas_path = 'gfci_canvas_path';
	$gfci_socialbar_pos = 'gfci_socialbar_pos';
	$gfci_socialbar_com = 'gfci_socialbar_com';
	$gfci_socialbar_capply = 'gfci_socialbar_capply';
	$gfci_socialbar_id = 'gfci_socialbar_id';
	$gfci_socialbar_comtext = 'gfci_socialbar_comtext';
	$gfci_socialbar_comheader = 'gfci_socialbar_comheader';
	$gfci_socialbar_youtube = 'gfci_socialbar_youtube';
	$gfci_socialbar_anon = 'gfci_socialbar_anon';
	$hidden_field_name = 'gfci_submit_hidden';
	$gfci_site_id = 'gfci_side_id';
	$gfci_canvas_path2 = 'gfci_canvas_path';
	$gfci_social_bar2 = 'gfci_social_bar';
	$gfci_socialbar_pos2 = 'gfci_socialbar_pos';
	$gfci_socialbar_com2 = 'gfci_socialbar_com';
	$gfci_socialbar_capply2 = 'gfci_socialbar_capply';
	$gfci_socialbar_id2 = 'gfci_socialbar_id';
	$gfci_socialbar_comtext2 = 'gfci_socialbar_comtext';
	$gfci_socialbar_comheader2 = 'gfci_socialbar_comheader';
	$gfci_socialbar_youtube2 = 'gfci_socialbar_youtube';
	$gfci_socialbar_anon2 = 'gfci_socialbar_anon';
	$opt_val = get_option( $gfci_site_id );
	
	if( $_POST[ $hidden_field_name ] == 'Y' ) {
		$opt_val = attribute_escape($_POST[ $gfci_site_id ]);
		$opt2_val = attribute_escape($_POST[ $gfci_canvas_path2 ]);
		$opt3_val = attribute_escape($_POST[ $gfci_social_bar2 ]);
		$opt4_val = attribute_escape($_POST[ $gfci_socialbar_pos2 ]);
		$opt5_val = attribute_escape($_POST[ $gfci_socialbar_com2 ]);
		$opt6_val = attribute_escape($_POST[ $gfci_socialbar_capply2 ]);
		$opt7_val = attribute_escape($_POST[ $gfci_socialbar_id2 ]);
		$opt8_val = attribute_escape($_POST[ $gfci_socialbar_comtext2 ]);
		$opt9_val = attribute_escape($_POST[ $gfci_socialbar_comheader2 ]);
		$opt10_val = attribute_escape($_POST[ $gfci_socialbar_youtube2 ]);
		$opt11_val = attribute_escape($_POST[ $gfci_socialbar_anon2 ]);
		update_option( $gfci_opt, $opt_val );
		update_option( $gfci_canvas_path, $opt2_val );
		update_option( $gfci_social_bar, $opt3_val );
		update_option( $gfci_socialbar_pos, $opt4_val );
		update_option( $gfci_socialbar_com, $opt5_val );
		update_option( $gfci_socialbar_capply, $opt6_val );
		update_option( $gfci_socialbar_id, $opt7_val );
		update_option( $gfci_socialbar_comtext, $opt8_val );
		update_option( $gfci_socialbar_comheader, $opt9_val );
		update_option( $gfci_socialbar_youtube, $opt10_val );
		update_option( $gfci_socialbar_anon, $opt11_val );
		?>
		<div class="updated"><p><strong><?php _e('Options saved.', 'gfci_trans_domain' ); ?></strong></p></div>
		<?php

	}
	if (isset( $gfci_social_bar2 )) {
		$opt3_val = get_option( $gfci_social_bar2 );
	} else { $opt3_val = 'false'; }
	if (isset( $gfci_canvas_path2 )) {
		$opt2_val = get_option( $gfci_canvas_path2 );
	} else { $opt2_val = '/'; }
	if (isset( $gfci_socialbar_pos2 )) {
		$opt4_val = get_option( $gfci_socialbar_pos2 );
	} else { $opt4_val = 'top'; }
	if (isset( $gfci_socialbar_com2 )) {
		$opt5_val = get_option( $gfci_socialbar_com2 );
	} else { $opt5_val = 'true'; }
	if (isset( $gfci_socialbar_capply2 )) {
		$opt6_val = get_option( $gfci_socialbar_capply2 );
	} else { $opt6_val = 'SITE'; }
	if (isset( $gfci_socialbar_id2 )) {
		$opt7_val = get_option( $gfci_socialbar_id2 );
	} else { $opt7_val = ''; }
	if (isset( $gfci_socialbar_comtext2 )) {
		$opt8_val = get_option( $gfci_socialbar_comtext2 );
	} else { $opt8_val = '- add your comment here -'; }
	if (isset( $gfci_socialbar_comheader2 )) {
		$opt9_val = get_option( $gfci_socialbar_comheader2 );
	} else { $opt9_val = 'Comments'; }
	if (isset( $gfci_socialbar_youtube2 )) {
		$opt10_val = get_option( $gfci_socialbar_youtube2 );
	} else { $opt10_val = 'true'; }
	if (isset( $gfci_socialbar_anon2 )) {
		$opt11_val = get_option( $gfci_socialbar_anon2 );
	} else { $opt11_val = 'true'; }
	echo '<div class="wrap">';
	echo "<h2>" . __( 'Google Friend Connect Integration Options', 'gfci_trans_domain' ) . "</h2>";
		    
	?>

	<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
	<?php wp_nonce_field('update-options'); ?>
	<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y" />
	<h3>Main Settings</h3>
	<p><label>Friend Connect SiteId:
	<input type="text" name="<?php echo $gfci_site_id; ?>" value="<?php echo $opt_val; ?>" size="20" />
	</label></p>
	<p><label>Location of rpc_relay.html and canvas.html 
	<input type="text" name="<?php echo $gfci_canvas_path2; ?>" value="<?php echo $opt2_val; ?>" size="10" />
	<br /><font color="red">Only change if needed.</font><br/>For Example: http://www.your-site.net<b>/blog/</b><br />You Enter: <b>/blog/</b></label></p>
	<hr />
	<h3>Social Bar Options</h3>
	<p><label>Use the Friend Connect Social Bar?
	<input type="checkbox" name="<?php echo $gfci_social_bar2; ?>" value="true" <?php if ( "true" == $opt3_val ){ echo 'checked="checked"'; }?> />
	</label></p>
	<p><label>Social Bar Position:
	<select id="position" name="<?php echo $gfci_socialbar_pos2; ?>"><?php echo "<option value='top' ".($opt4_val=="top" ? "selected='selected'" : '').">Top</option>"; ?><?php echo "<option value='bottom' ".($opt4_val=="bottom" ? "selected='selected'" : '').">Bottom</option>"; ?></select>
	</label></p>
	<p><b>Important:</b>  If you're using the bottom position you may need to add<br /> the following code to the very top of your themes header file.  Should work fine for most without doing this.<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</code></p>
	<p><label>Include Comment Gadget?
	<input type="checkbox" name="<?php echo $gfci_socialbar_com2; ?>" value="true" <?php if ( "true" == $opt5_val ){ echo 'checked="checked"'; }?> />
	</label></p>
	<p><label>Comments Apply To?
	<select id="commentapply" name="<?php echo $gfci_socialbar_capply2; ?>"><?php echo "<option value='SITE' ".($opt6_val=="SITE" ? "selected='selected'" : '').">Entire Site</option>"; ?><?php echo "<option value='PAGE' ".($opt6_val=="PAGE" ? "selected='selected'" : '').">Page</option>"; ?><?php echo "<option value='ID' ".($opt6_val=="ID" ? "selected='selected'" : '').">Item</option>"; ?></select>
	</label></p>
	<p><label>If comments apply to an item, enter it's ID here:
	<input type="text" name="<?php echo $gfci_socialbar_id2; ?>" value="<?php echo $opt7_val; ?>" size="10" />
	</label></p>
	<p><label>Default Comment Text:
	<input type="text" name="<?php echo $gfci_socialbar_comtext2; ?>" value="<?php echo $opt8_val; ?>" size="20" />
	</label></p>
	<p><label>Default Comment Header:
	<input type="text" name="<?php echo $gfci_socialbar_comheader2; ?>" value="<?php echo $opt9_val; ?>" size="10" />
	</label></p>
	<p><label>Allow YouTube Links?
	<input type="checkbox" name="<?php echo $gfci_socialbar_youtube2; ?>" value="video," <?php if ( "video," == $opt10_val ){ echo 'checked="checked"'; } ?> />
	</label></p>
<?php /*
	<p><label>Allow Anonymous Posts?
	<input type="checkbox" name="<?php echo $gfci_socialbar_anon2; ?>" value="true" <?php if ( "true" == $opt11_val ){ echo 'checked="checked"'; } ?> />
	</label></p>
	*/?>
	<hr />
	<p>If you wish to use Friend Connect gadgets in your sidebars<br />
	add them and set their options through your WordPress widgets.</p>	
	<hr />
	<p>Confused? The <a href="http://www.socialarrow.com/2009/01/21/screenshots-gfc-integration-wordpress/">GFCI screenshot page</a> might help.</p>
	<p class="submit">
	<input type="submit" name="Submit" value="<?php _e('Update Options', 'gfci_trans_domain' ) ?>" />
	</p>
	</form>
	</div>

	<?php
 
}

function gfci_shortcode($atts, $content = null ) {
    attribute_escape(extract(shortcode_atts(array(	'gadget'	=> 'comment',
	    				'scope'		=> 'SITE',
					'video'		=> 'true',
					'anon'		=> 'true',
					'id'		=> '',
	    				'width'		=> '230',
					'ebackc'   	=> 'e0ecff',
					'etextc'	=> '333333',
					'elinkc'	=> '0000cc',
					'altbackc'	=> 'e0ecff',
					'cbackc'	=> 'ffffff',
					'clinkc'	=> '0000cc',
					'ctextc'	=> '333333',
					'cslinkc'	=> '7777cc',
					'cstextc'	=> '666666',
					'cheadc'	=> '333333',
					'align'		=> 'center',
					'oborderc'	=> 'cccccc',
					'iborderc'	=> 'cccccc',
					'dtext' 	=> '- enter text here -',
					'dheader'	=> 'Comments:',
					'maxim'		=> 'true',
					'posts'		=> '5',
					'gadgeturl'	=> 'http://os.ilike.com/gadget/playlist',
					'present'	=> ''), $atts)));
    $site_id 	= get_option('gfci_side_id');
    $cpath	= get_option('gfci_canvas_path');
    if ( "true" == $video) { $video2 = 'video,'; } else {$video2 = ''; }
    if ( "center" == $align ) { $align2 = 'margin-left: auto; margin-right: auto;'; }
    elseif ( "left" == $align ) { $align2 = null; }
    else { $align2 = 'float:right;'; }
    if ( "canvas" == $present ) { $present2 = 'presentation:"canvas",'; }
    
    
    if ( "comment" == $gadget ) {    
	    $divid2 = rand(1,1000000);
	    return '<div id="gfcidiv-' . $divid2 . '" style="' . $align2 . ' width:
	    	' . $width . 'px;border:1px solid #' . $oborderc . ';"></div>
		<div style="clear:both;"></div><br/><script type="text/javascript">
		var skin = {};
		skin["BORDER_COLOR"] = "#' . $iborderc . '";
		skin["ENDCAP_BG_COLOR"] = "#' . $ebackc . '";
		skin["ENDCAP_TEXT_COLOR"] = "#' . $etextc . '";
		skin["ENDCAP_LINK_COLOR"] = "#' . $elinkc . '";
		skin["ALTERNATE_BG_COLOR"] = "#' . $altbackc . '";
		skin["CONTENT_BG_COLOR"] = "#' . $cbackc . '";
		skin["CONTENT_LINK_COLOR"] = "#' . $clinkc . '";
		skin["CONTENT_TEXT_COLOR"] = "#' . $ctextc . '";
		skin["CONTENT_SECONDARY_LINK_COLOR"] = "#' . $cslinkc . '";
		skin["CONTENT_SECONDARY_TEXT_COLOR"] = "#' . $cstextc . '";
		skin["CONTENT_HEADLINE_COLOR"] = "#' . $cheadc . '";
		skin["DEFAULT_COMMENT_TEXT"] = "' . $dtext . '";
		skin["HEADER_TEXT"] = "' . $dheader .'";
		skin["POSTS_PER_PAGE"] = "' . $posts . '";
		google.friendconnect.container.setParentUrl("' . $cpath . '" /* location of rpc_relay.html and canvas.html */);
		google.friendconnect.container.renderWallGadget(
			{ id: "gfcidiv-' . $divid2 . '",
			site: "' . $site_id . '",
			"view-params":{"scope":"' . $scope . '","allowAnonymousPost":"' . $anon . '","features":"' . $video2 . 'comment","docId":"' . $id . '"}
			},
			skin);
			</script>';
    }
    
    if ( "review" == $gadget ) {
	    $divid2 = rand(1,1000000);
	    return '<div id="gfcidiv-' . $divid2 . '" style="' . $align2 . ' width:
	    	' . $width . 'px;border:1px solid #' . $oborderc . ';"></div>
		<div style="clear:both;"></div><script type="text/javascript">
		var skin = {};
		skin["BORDER_COLOR"] = "#' . $iborderc . '";
		skin["ENDCAP_BG_COLOR"] = "#' . $ebackc . '";
		skin["ENDCAP_TEXT_COLOR"] = "#' . $etextc . '";
		skin["ENDCAP_LINK_COLOR"] = "#' . $elinkc . '";
		skin["ALTERNATE_BG_COLOR"] = "#' . $altbackc . '";
		skin["CONTENT_BG_COLOR"] = "#' . $cbackc . '";
		skin["CONTENT_LINK_COLOR"] = "#' . $clinkc . '";
		skin["CONTENT_TEXT_COLOR"] = "#' . $ctextc . '";
		skin["CONTENT_SECONDARY_LINK_COLOR"] = "#' . $cslinkc . '";
		skin["CONTENT_SECONDARY_TEXT_COLOR"] = "#' . $cstextc . '";
		skin["CONTENT_HEADLINE_COLOR"] = "#' . $cheadc . '";
		skin["DEFAULT_COMMENT_TEXT"] = "' . $dtext . '";
		skin["HEADER_TEXT"] = "' . $dheader .'";
		skin["POSTS_PER_PAGE"] = "' . $posts . '";
		google.friendconnect.container.setParentUrl("' . $cpath . '" /* location of rpc_relay.html and canvas.html */);
		google.friendconnect.container.renderReviewGadget(
			{ id: "gfcidiv-' . $divid2 . '",
			site: "' . $site_id . '",
			"view-params":{"disableMinMax":"false","scope":"' . $scope . '","allowAnonymousPost":"' . $anon . '","docId":"' . $id . '","startMaximized":"' . $maxim . '"}
			},
			skin);
			</script>';
    }
    if ( "custom" == $gadget ) {
	    $divid2 = rand(1,1000000);
	    return '<div id="gfcidiv-' . $divid2 . '" style="' . $align2 . ' width:
	    ' . $width . 'px;border:1px solid #' . $oborderc . ';"></div>
	    <div style="clear:both;"></div><script type="text/javascript">
	    google.friendconnect.container.setParentUrl("'. $cpath . '" /* location of rpc_relay.html and canvas.html */);
	    google.friendconnect.container.renderOpenSocialGadget(
	    { id: "gfcidiv-' . $divid2 . '", ' . $present2 . 'url:"' . $gadgeturl . '",
	    site: "' . $site_id . '" });
	    </script>';
    }	    
    return;
}

function gfci_socialbar_display() {
	$opt_val = get_option('gfci_social_bar');
	if($opt_val == 'true') {
		$gfci_opt = get_option('gfci_side_id');
		$gfci_social_bar = get_option('gfci_social_bar');
		$gfci_canvas_path = get_option('gfci_canvas_path');
		$gfci_socialbar_pos = get_option('gfci_socialbar_pos');
		$gfci_socialbar_com = get_option('gfci_socialbar_com');
		$gfci_socialbar_capply = get_option('gfci_socialbar_capply');
		$gfci_socialbar_id = get_option('gfci_socialbar_id');
		$gfci_socialbar_comtext = get_option('gfci_socialbar_comtext');
		$gfci_socialbar_comheader = get_option('gfci_socialbar_comheader');
		$gfci_socialbar_youtube = get_option('gfci_socialbar_youtube');
		$gfci_socialbar_anon = get_option('gfci_socialbar_anon');
		
		if ( "true" == $gfci_socialbar_com ) { $gfci_socialbar_com2 = 'true'; } else { $gfci_socialbar_com2 = 'false'; }
		if ( "true" == $gfci_socialbar_anon ) { $gfci_socialbar_anon2 = 'true'; } else { $gfcisocialbar_anon2 = 'false'; }
		if ( "ID" == $gfci_socialbar_capply ) { $gfci_socialbar_id2 = $gfci_socialbar_id; } else { $gfcisocialbar_id2 = ''; }
		?>
		<div id="div-1234681839482"></div>
		<!-- Render the gadget into a div. -->
		<script type="text/javascript">
		var skin = {};
		skin['BORDER_COLOR'] = '#cccccc';
		skin['ENDCAP_BG_COLOR'] = '#e0ecff';
		skin['ENDCAP_TEXT_COLOR'] = '#333333';
		skin['ENDCAP_LINK_COLOR'] = '#0000cc';
		skin['ALTERNATE_BG_COLOR'] = '#ffffff';
		skin['CONTENT_BG_COLOR'] = '#ffffff';
		skin['CONTENT_LINK_COLOR'] = '#0000cc';
		skin['CONTENT_TEXT_COLOR'] = '#333333';
		skin['CONTENT_SECONDARY_LINK_COLOR'] = '#7777cc';
		skin['CONTENT_SECONDARY_TEXT_COLOR'] = '#666666';
		skin['CONTENT_HEADLINE_COLOR'] = '#333333';
		skin['POSITION'] = '<?php echo $gfci_socialbar_pos; ?>';
		skin['DEFAULT_COMMENT_TEXT'] = '<?php echo $gfci_socialbar_comtext; ?>';
		skin['HEADER_TEXT'] = '<?php echo $gfci_socialbar_comheader; ?>';
		google.friendconnect.container.setParentUrl('<?php echo $gfci_canvas_path; ?>' /* location of rpc_relay.html and canvas.html */);
		google.friendconnect.container.renderSocialBar(
			{ id: 'div-1234681839482',
				site: '<?php echo $gfci_opt; ?>',
				'view-params':{"scope":"<?php echo $gfci_socialbar_capply; ?>","allowAnonymousPost":"false","features":"<?php echo $gfci_socialbar_youtube; ?>comment","showWall":"<?php echo $gfci_socialbar_com2; ?>","docId":"<?php echo $gfci_socialbar_id2; ?>"}
			},
			skin);
		</script>
		<?php
	}
}

function myGFCI_init() {
	register_sidebar_widget(__('GFC Members'), 'GFCIWidget_Members');
	register_sidebar_widget(__('GFC Login'), 'GFCIWidget_Login');
	register_widget_control(__('GFC Members'), 'widget_gfci_member_control', 420);
	register_widget_control(__('GFC Login'), 'widget_gfci_login_control', 420);
}

add_action('wp_head', 'gfci_header');
add_action('admin_menu', 'gfc_plugin_menu');
add_action('widgets_init', 'myGFCI_init');
add_action('widgets_init', 'gfci_wall_multi_register');
add_action('widgets_init', 'gfci_review_multi_register');
add_action('widgets_init', 'gfci_custom_multi_register');
add_action('wp_footer', 'gfci_socialbar_display');
add_shortcode('gfc', 'gfci_shortcode');

?>
